import sys
sys.path.append("../utils")

config_path = "./config.cfg"
import ConfigParser
import time

import tables as tb
import numpy as np

import utilsabbo as util
import data_utilsabbo as dutil


def get_data_from_csv_to_h5(turbine, file_name):
    config = ConfigParser.ConfigParser()
    config.read(config_path)
    csv_path = config.get('options', 'csv_path')
    fil_str = csv_path + file_name + ".csv"

    # Read header lines
    f = open(fil_str, 'r')
    name_list = f.readline()\
                            .replace(turbine + '-', '')\
                            .replace('\n', '')\
                            .split(';')

    info_str = f.readline().replace('\n', '').split(';')
    tid = f.readline().replace('\n', '').split(';')[0]
    f.close()

    num_of_meas = len(name_list)

    c = np.arange(0, num_of_meas, 2)

    new_name = []
    for name in name_list:
        if len(name) > 0:
            new_name.append(name)

    name_list = new_name

    dtyp = []
    for pos in c:
        time_label = name_list[pos] + 'Time'
        name_list.insert(pos, time_label)
        info_str[pos] = name_list[pos + 1]
        dtyp.append(float)
        dtyp.append(float)

    info = dict(zip(info_str[::2], info_str[1::2]))

    # If the time contains seconds, use special converter function
    if len(tid) > 15:
        convs = dict((col, util.mkdatess2) for col in c)
    else:
        convs = dict((col, util.mkdate2) for col in c)

    # Removing columns with no data.
    cols_to_use, info = dutil.remove_type_from_data_set(info, info_str, 'WindDirAbsoluteAvg')
    data = np.genfromtxt(fil_str,
                         delimiter=";",
                         names=name_list,
                         skip_header=2,
                         usecols= cols_to_use,
                         converters=convs,
                         dtype=dtyp)

    good_times = dutil.find_good_times(data, info)
    good_data = dutil.construct_good_data(data, info, good_times)
    name_list = info.keys()
    name_list.insert(0, "Time")
    construct_db_class(name_list)
    write_to_h5(turbine, good_data, name_list)
    # Clearing some of all the memory used
    del data

def construct_db_class(name_list):
    """
    This functions generates a file with the class
    used for constructing the PyTables database
    """
    config = ConfigParser.ConfigParser()
    config.read(config_path)
    db_class_file = config.get('options', 'db_class_file')

    f = open(db_class_file, 'w')
    f.write("import tables as tb\n")
    f.write("class Particle(tb.IsDescription):\n")
    for name in name_list:
        f.write(("    " + name + " = tb.Float64Col()\n"))
    return

def write_to_h5(turbine, good_data, name_list):
    config = ConfigParser.ConfigParser()
    config.read(config_path)
    bin_path = config.get('options', 'bin_path')
    h5file = config.get('options', 'h5file')

    import db_class as db_cl

    filters = tb.Filters(complevel=2, complib="blosc")
    h5file = tb.openFile((bin_path + h5file), mode='a',
            title="SCADA Data", filters=filters)

    group = h5file.root

    if turbine in h5file.root._v_children.keys():
        table = h5file.root._v_children[turbine]
    else:
        table = h5file.createTable(group, turbine, db_cl.Particle)
    name_data = zip(name_list, good_data)
    name_data.sort()
    names, data = zip(*name_data)
    table.append(data)
    h5file.close()
    return
    group = h5file.createGroup("/", turbine , turbine + 'Detector information')
    for i, name in enumerate(name_list):
        h5file.createArray(group, name, good_data[i])
    h5file.close()

    # Clearing some of all the memory used
    del good_data

def main():
    start_time = time.time()
    config = ConfigParser.ConfigParser()
    config.read(config_path)
    turbines = config.get('options', 'turbines').split(',')
    years = config.get('options', 'years').split(',')
    meas_type = config.get('options', 'meas_type')
    #data_date = config.get('options', 'data_date')
    print "Getting data from the following turbines"
    print turbines
    for year in years:
        print year
        for turbine in turbines:
            file_name = meas_type + turbine + year
            get_data_from_csv_to_h5(turbine, file_name)
            print turbine + " loaded "
    print "Done in:",
    print (time.time() - start_time)

if __name__=='__main__':
    main()

